#import "ALNavigationHelper.h"


@implementation ALNavigationHelper

+(void)push:(UINavigationController*)naviController controller:(UIViewController*)controller{
    [naviController pushViewController:controller animated:YES];
}
+(void)push:(UINavigationController*)naviController controller:(UIViewController*)controller isAnimated:(BOOL)isAnimated{
   [naviController pushViewController:controller animated:isAnimated];
}

+(void)pop:(UINavigationController*)naviController{
    [naviController popViewControllerAnimated:YES];
}
+(void)pop:(UINavigationController*)naviController isAnimated:(BOOL)isAnimated{
    [naviController popViewControllerAnimated:isAnimated];
}

+ (UINavigationController *)currentNavigationController{
    if (![[UIApplication sharedApplication].windows.lastObject isKindOfClass:[UIWindow class]]) {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self currentNavigationControllerFrom:rootViewController];
}

//递归
+ (UINavigationController *)currentNavigationControllerFrom:(UIViewController *)vc{
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UINavigationController *nc = ((UITabBarController *)vc).selectedViewController;
        return [self currentNavigationControllerFrom:nc];
    }else if ([vc isKindOfClass:[UINavigationController class]]) {
        if (((UINavigationController *)vc).presentedViewController) {
            return [self currentNavigationControllerFrom:((UINavigationController *)vc).presentedViewController];
        }
        return [self currentNavigationControllerFrom:((UINavigationController *)vc).topViewController];
    }else if ([vc isKindOfClass:[UIViewController class]]) {
        if (vc.presentedViewController) {
            return [self currentNavigationControllerFrom:vc.presentedViewController];
        }
        else {
            return vc.navigationController;
        }
    }
    else {
        NSAssert(0, @"未获取到导航控制器");
        return nil;
    }
}



@end
